using System;
using System.Globalization;
using ns20;

namespace Newtonsoft.Json.Converters
{
	public class IsoDateTimeConverter : DateTimeConverterBase
	{
		private const string string_0 = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";

		private DateTimeStyles dateTimeStyles_0 = DateTimeStyles.RoundtripKind;

		private string string_1;

		private CultureInfo cultureInfo_0;

		public DateTimeStyles DateTimeStyles
		{
			get
			{
				return dateTimeStyles_0;
			}
			set
			{
				dateTimeStyles_0 = value;
			}
		}

		public string DateTimeFormat
		{
			get
			{
				return string_1 ?? string.Empty;
			}
			set
			{
				string_1 = Class199.smethod_5(value);
			}
		}

		public CultureInfo Culture
		{
			get
			{
				return cultureInfo_0 ?? CultureInfo.CurrentCulture;
			}
			set
			{
				cultureInfo_0 = value;
			}
		}

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			string value2;
			if (value is DateTime)
			{
				DateTime dateTime = (DateTime)value;
				if ((dateTimeStyles_0 & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal || (dateTimeStyles_0 & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
				{
					dateTime = dateTime.ToUniversalTime();
				}
				value2 = dateTime.ToString(string_1 ?? "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK", Culture);
			}
			else
			{
				if (!(value is DateTimeOffset))
				{
					throw new JsonSerializationException("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".smethod_0(CultureInfo.InvariantCulture, Class195.smethod_3(value)));
				}
				DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
				if ((dateTimeStyles_0 & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal || (dateTimeStyles_0 & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
				{
					dateTimeOffset = dateTimeOffset.ToUniversalTime();
				}
				value2 = dateTimeOffset.ToString(string_1 ?? "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK", Culture);
			}
			writer.WriteValue(value2);
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			bool flag;
			Type type = ((flag = Class195.smethod_10(objectType)) ? Nullable.GetUnderlyingType(objectType) : objectType);
			if (reader.JsonToken_0 == JsonToken.Null)
			{
				if (!Class195.smethod_10(objectType))
				{
					throw JsonSerializationException.smethod_1(reader, "Cannot convert null value to {0}.".smethod_0(CultureInfo.InvariantCulture, objectType));
				}
				return null;
			}
			if (reader.JsonToken_0 == JsonToken.Date)
			{
				if (type == typeof(DateTimeOffset))
				{
					return new DateTimeOffset((DateTime)reader.Object_0);
				}
				return reader.Object_0;
			}
			if (reader.JsonToken_0 != JsonToken.String)
			{
				throw JsonSerializationException.smethod_1(reader, "Unexpected token parsing date. Expected String, got {0}.".smethod_0(CultureInfo.InvariantCulture, reader.JsonToken_0));
			}
			string text = reader.Object_0.ToString();
			if (string.IsNullOrEmpty(text) && flag)
			{
				return null;
			}
			if (type == typeof(DateTimeOffset))
			{
				if (!string.IsNullOrEmpty(string_1))
				{
					return DateTimeOffset.ParseExact(text, string_1, Culture, dateTimeStyles_0);
				}
				return DateTimeOffset.Parse(text, Culture, dateTimeStyles_0);
			}
			if (!string.IsNullOrEmpty(string_1))
			{
				return DateTime.ParseExact(text, string_1, Culture, dateTimeStyles_0);
			}
			return DateTime.Parse(text, Culture, dateTimeStyles_0);
		}
	}
}
